package org.smoozik.services.musictheory;

import org.smoozik.app.ihm.Constants;

public class Interval {

	private int degre;
	private int nbHalfTons;

	public Interval(int degre, int nbHalfTons) {
		this.degre = degre;
		this.nbHalfTons = nbHalfTons;
	}

	public Interval(Interval i) {
		this(i.getDegre(), i.getNbHalfTons());
	}

	public Interval(int nbHalfTons) {
		this.nbHalfTons = nbHalfTons;
		this.degre = getIndexFromHauteur();
	}

	public int getIndexFromHauteur() {
//		int degre = 0;
//		
//		for (int i = 0; i < INTERVALS.length; i++) {
//			if(this.nbHalfTons == INTERVALS[i].getNbHalfTons()) {
//				degre = INTERVALS[this.nbHalfTons].getDegre();
//				break;
//			}
//		}
//		
		return Constants.INTERVALS[this.nbHalfTons].getDegre();
	}

	public int getHauteurNonalteree() {
		int rest = this.degre % 7;
		int div = this.degre / 7;

		if (rest < 0) {
			rest += 7;
			div -= 1;
		}

		int h = Constants.indexValues[rest] + (div) * 12;

		return h;
	}

	public void add(Interval i) {
		this.degre += i.getDegre();
		this.nbHalfTons += i.getNbHalfTons();
	}

	public void revert() {
		this.degre = 7 - this.degre;
		this.nbHalfTons = 12 - this.nbHalfTons;
	}

	public int getNbHalfTons() {
		return nbHalfTons;
	}

	public void setNbHalfTons(int nbHalfTons) {
		this.nbHalfTons = nbHalfTons;
	}

	public int getDegre() {
		return degre;
	}

	public void setDegre(int degre) {
		this.degre = degre;
	}

	public boolean equals(Interval i) {
		boolean result = false;

		if (i != null) {
			if (i.getDegre() == this.degre && i.getNbHalfTons() == nbHalfTons) {
				result = true;
			}
		}

		return result;
	}

	public String toString() {
		String lString = "";

		for (int i = 0; i < Constants.INTERVALS.length; i++) {
			if(this.equals(Constants.INTERVALS[i])) {
				lString = Constants.INTERVAL_NAMES[i];
				break;
			}
			if(this.equals(Constants.INTERVALS[i])) {
				lString = Constants.INTERVAL_NAMES[i];
				break;
			}
		}

		return lString;
	}
}
